/**
 * 
 */
package sailing.ui.base;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.jdesktop.application.SingleFrameApplication;

import cjlite.logger.Logger;

/**
 * @author ming
 * 
 */
public abstract class CommonJDialog extends AbstractJDialog {

	private static Logger logger = Logger.thisClass();

	private Dimension topDim = new Dimension(500, 50);
	private Dimension bottomDim = new Dimension(500, 50);

	private static Color topBgColor = new Color(231, 241, 254);
	private static Color bottomBgColor = new Color(240, 240, 240);
	private static Font topFont = new Font("微软雅黑", Font.PLAIN, 18);
	private JLabel msgLabel;

	/**
	 * @param window
	 * @param title
	 * @param applicationModal
	 * @param singleFrameApplication
	 */
	public CommonJDialog(Window window, String title, ModalityType applicationModal,
			SingleFrameApplication singleFrameApplication) {
		super(window, title, applicationModal, singleFrameApplication);
	}

	protected void initialUI() {
		JPanel root = new JPanel();
		root.setLayout(new BorderLayout());
		root.add(this.initialTopPanel(), BorderLayout.NORTH);
		root.add(this.initialContentPanel(), BorderLayout.CENTER);

		String[] bottomBtnText = this.getBottomBtnTxt();
		if (bottomBtnText != null) {
			root.add(this.initialBottomBtnBar(bottomBtnText), BorderLayout.SOUTH);
		}

		this.setLayout(new BorderLayout());
		this.add(root);
		this.addWindowListener(new WindowAdapter() {

			@Override
			public void windowOpened(WindowEvent e) {
				updateTopMessage();
				preUpdateUIData();

			}

		});
	}

	protected void updateTopMessage() {
		String msg = this.getTopMessage();
		if (msg == null) {
			msg = this.getTitle();
		}
		this.msgLabel.setText(msg);
	}

	protected void preUpdateUIData() {

	}

	private JPanel initialBottomBtnBar(String[] bottomBtnText) {
		JPanel bottom = new JPanel() {

			@Override
			public Dimension getPreferredSize() {
				return bottomDim;
			}

			@Override
			public Dimension getMaximumSize() {
				return bottomDim;
			}

			@Override
			public Dimension getMinimumSize() {
				return bottomDim;
			}
		};

		bottom.setBackground(bottomBgColor);
		bottom.setBorder(BorderFactory.createMatteBorder(2, 0, 0, 0, Color.DARK_GRAY));

		Box allBox = Box.createVerticalBox();
		allBox.add(Box.createVerticalStrut(7));

		Box hbox = Box.createHorizontalBox();
		hbox.add(Box.createHorizontalGlue());
		for (int i = 0; i < bottomBtnText.length; i++) {
			hbox.add(Box.createHorizontalStrut(30));
			JButton btn = new JButton(bottomBtnText[i]);
			hbox.add(btn);
			btn.setMargin(this.getButtonInsets());
			btn.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					if (e.getSource() instanceof JButton) {
						JButton btn = (JButton) e.getSource();
						logger.trace("Button with text[{0}] clicked", btn.getText());
						performBtnClick(btn.getText());
					}
				}
			});
		}
		hbox.add(Box.createHorizontalStrut(50));
		allBox.add(hbox);

		allBox.add(Box.createVerticalStrut(20));

		bottom.setLayout(new BorderLayout());
		bottom.add(allBox, BorderLayout.CENTER);
		return bottom;
	}

	private Insets getButtonInsets() {
		return new Insets(3, 15, 3, 15);
	}

	protected void performBtnClick(String btnCaption) {

	}

	protected String[] getBottomBtnTxt() {
		return null;
	}

	private JPanel initialTopPanel() {
		JPanel top = new JPanel() {

			@Override
			public Dimension getPreferredSize() {
				return topDim;
			}

			@Override
			public Dimension getMaximumSize() {
				return topDim;
			}

			@Override
			public Dimension getMinimumSize() {
				return topDim;
			}
		};

		top.setBackground(topBgColor);
		top.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.DARK_GRAY));

		Box box = Box.createVerticalBox();
		box.add(Box.createVerticalStrut(15));
		Box hbox = Box.createHorizontalBox();
		hbox.add(Box.createHorizontalStrut(30));

		String msg = this.getTopMessage();
		if (msg == null) {
			msg = this.getTitle();
		}
		msgLabel = new JLabel(msg);
		msgLabel.setFont(topFont);
		hbox.add(msgLabel);
		hbox.add(Box.createHorizontalGlue());
		box.add(hbox);
		box.add(Box.createVerticalStrut(15));

		top.setLayout(new BorderLayout());
		top.add(box, BorderLayout.CENTER);
		return top;
	}

	protected String getTopMessage() {
		return null;
	}

	protected abstract JPanel initialContentPanel();

}
